<!-- 合作市场列表 -->
<template>
  <a-card :bordered="false" class="br-10">
    <div class="table-page-search">
      <a-form>
        <a-form-item label="申请合作机构">
          <a-input v-model="queryParam.organName" />
        </a-form-item>
        <a-form-item label="合作资源">
          <a-select v-model="queryParam.source" allowClear>
            <a-select-option v-for="item in sourceList" :value="item.value" :key="item.value">{{
              item.label
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <span class="search-submitButtons">
          <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
          <a-button @click="handleReset">重置</a-button>
        </span>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      rowKey="id, enabled, title"
      :columns="columns"
      :data="loadData"
      :rowSelection="null"
    >
      <!-- 序号 -->
      <span slot="serial" slot-scope="text, record, index">
        {{ (pagination.current - 1) * pagination.pageSize + parseInt(index) + 1 }}
      </span>
      <!-- 组织类型 -->
      <span slot="orgType" slot-scope="text">
        {{ getOrgTypeText(text) }}
      </span>
      <!-- 合作情况 -->
      <span slot="cooperated" slot-scope="text"> {{ text }}家机构 </span>
      <!-- 合作资源 -->
      <span slot="pronum" slot-scope="text, record">
        <a @click="handleShowSourceModal(record)">{{ text }}项</a>
      </span>
      <!-- 组织主页 -->
      <span slot="home" slot-scope="text, record">
        <a @click="handleShowHomeModal(record)">查看主页</a>
      </span>
      <!-- 操作按钮 -->
      <span slot="action" slot-scope="text, record">
        <template>
          <span v-if="text">已合作</span>
          <a v-else @click="handleApply(record)">申请合作</a>
        </template>
      </span>
    </s-table>
    <Home ref="home" />
    <Source ref="source" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { MarketList, sourceList, CheckPay } from '@/api/business/cooperation'
import { getDictMap } from '@/utils/dict/dict'
import Home from './modal/home.vue'
import Source from './modal/source.vue'
import Vue from 'vue'
import { TENANTID } from '@/store/mutation-types'
export default {
  components: {
    STable,
    Home,
    Source,
  },
  data() {
    return {
      pagination: {
        current: 1,
        pageSize: 10,
      },
      queryParam: {
        organName: undefined,
        source: undefined,
      },
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width: '80px',
        },
        {
          title: '组织名称',
          dataIndex: 'orgName',
          ellipsis: true,
          width: '200px',
        },
        {
          title: '组织类型',
          dataIndex: 'orgType',
          ellipsis: true,
          width: '100px',
          scopedSlots: { customRender: 'orgType' },
        },
        {
          title: '合作情况',
          dataIndex: 'cooperated',
          ellipsis: true,
          width: '150px',
          scopedSlots: { customRender: 'cooperated' },
        },
        {
          title: '合作资源',
          dataIndex: 'pronum',
          ellipsis: true,
          width: '150px',
          scopedSlots: { customRender: 'pronum' },
        },
        {
          title: '组织主页',
          ellipsis: true,
          width: '150px',
          scopedSlots: { customRender: 'home' },
        },
        {
          title: '操作',
          width: '180px',
          dataIndex: 'isF',
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: async (parameter) => {
        var params = {
          current: parameter.pageNum,
          size: parameter.pageSize,
        }
        return MarketList(Object.assign(params, this.queryParam))
          .then((res) => {
            if (res.code === 0) {
              this.pagination.current = parameter.pageNum
              this.pagination.pageSize = parameter.pageSize
              return res
            } else {
              this.$message.error(res.msg)
            }
          })
          .catch((err) => {
            this.$message.error(err.msg)
          })
      },
      orgTypeMap: {},
      sourceList,
      tenantId: Vue.ls.get(TENANTID),
    }
  },
  created() {
    this.GetDictMap()
  },
  methods: {
    handleShowHomeModal(record) {
      this.$refs.home.show(record)
    },
    handleShowSourceModal(record) {
      this.$refs.source.show(record)
    },
    handleReset() {
      Object.keys(this.queryParam).forEach((k) => {
        this.queryParam[k] = undefined
      })
      this.$refs.table.refresh(true)
    },
    async GetDictMap() {
      this.orgTypeMap = await getDictMap('org_type')
    },
    getOrgTypeText(val) {
      return this.orgTypeMap && this.orgTypeMap.get(val)
    },
    async handleApply(row) {
      let { code, msg } = await CheckPay(this.tenantId)
      if (code !== 0) return this.$message.error(msg)
      this.$router.push({
        path: '/business/cooperation/applyCooperation',
        query: {
          data: JSON.stringify(row),
        },
      })
    },
  },
}
</script>
<style scoped lang="less"></style>
